<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<%@ include file="../common/static.html" %>
<script type="text/javascript">
	var grid;

	//增加工具栏
	var toolbarOptions = {
		items : [ {
			text : '增加',
			id : 'add',
			click : toolbarBtnItemClick,
			icon : "add"
		}, {
			line : true
		}, {
			text : '修改',
			id : 'modify',
			click : toolbarBtnItemClick,
			icon : "modify"
		}, {
			line : true
		}, {
			text : '保存',
			id : 'save',
			click : toolbarBtnItemClick,
			icon : "save"
		}, {
			line : true
		}, {
			text : '取消',
			id : 'cancel',
			click : toolbarBtnItemClick,
			icon : "back"
		}, {
			line : true
		}, {
			text : '删除',
			id : 'delete',
			click : toolbarBtnItemClick,
			icon : "delete"
		} ]
	};

	$(function() {
		window.grid = $("#maingrid")
				.ligerGrid(
						{
							toolbar : toolbarOptions,
							columns : [
									{
										display : '序号',
										render : function(item, index){
											return(index+1);
										}
									},
									{
										display : '主键',
										name : 'id',
										type : 'int'//,
										//hide: true,
									},
									{
										display : '单价',
										name : 'price',
										type : 'float',
										editor: { type: 'float'}
									},
									{
										display : '日期',
										name : 'date',
										type : 'date',
										format: 'yyyy年MM月dd日',
										editor: { 
											format: 'yyyy-MM-dd',
											type: 'date'
										}
									},
									{
										display : '备注',
										name : 'note',
										editor: { type: 'text' }
									}],
							isScroll : false,
							checkbox : false,
							rownumbers : true,
							enabledEdit: true,
							//clickToEdit: true,
							url : 'getgoldprice',
							sortName : 'id',
							pageSize:10,
							dataAction:'local',
							title : '金价表'
						});
	});

	//工具条事件
	function toolbarBtnItemClick(item) {
		switch (item.id) {
		case "add":
			f_add();
			break;
		case "modify":
			var selected = grid.getSelected();
			if (!selected) {
				LG.tip('请选择行!');
				return
			}
			grid.beginEdit(selected);
			break;
		case "save":
			var selected = grid.getSelected();
			if (!selected) {
				LG.tip('请选择行!');
				return
			}
			grid.endEdit(selected);
			f_save(selected);
			break;
		case "cancel":
			var selected = grid.getSelected();
			if (!selected) {
				LG.tip('请选择行!');
				return
			}
			grid.cancelEdit(selected);
			break;
		case "delete":
			var selected = grid.getSelected();
			if (selected) {
				jQuery.ligerDialog.confirm('确定删除吗?', function(confirm) {
					if (confirm)
						f_delete();
				});
			} else {
				LG.tip('请选择行!');
			}
			break;
		}
	}

	function f_delete() {
		var selected = grid.getSelected();
		if(selected.id == ''){
			grid.deleteSelectedRow(selected);
			LG.showSuccess('删除成功');
		}else{
			LG.ajax({
				action : 'deletegoldprice',
				loading : '正在删除中...',
				data : {
					'tgpo.id' : selected.id
				},
				success : function() {
					LG.showSuccess('删除成功');
					f_reload();
				},
				error : function(message) {
					LG.showError(message);
				}
			});
		}
	}

	function f_reload() {
		grid.loadData();
	}
	
	function f_add(){
		grid.addRow({ 
                id: '',
                price: 0.0,
                date: getdate(),
                note: ''
            });
	}
	
	function f_save(selected){
		var selected = grid.getSelected(),
			data=$.mergeData('tgpo',selected);
		LG.ajax({
				action : 'savegoldprice',
				loading : '正在保存中...',
				data :data,
				success : function() {
					LG.showSuccess('保存成功');
					f_reload();
				},
				error : function(message) {
					LG.showError(message);
				}
			});
	}
</script>
</head>

<body>
	<div id="maingrid" style="margin:10px;"></div>
</body>
</html>
